#!/bin/bash
# $Id$
# vim: ts=2 syn=sh
# desc: host-initram/init for slackware

PATH=/sbin:/usr/sbin:/usr/local/sbin:/bin:/usr/bin ; export PATH
LD_LIBRARY_PATH=/lib:/usr/lib:/usr/local/lib ; export LD_LIBRARY_PATH

set -eE
shopt -s nocasematch

_chk_err ()
{
		local count
		echo " ERROR:  rebooting."
  	while true; do
  		echo -ne '\a'
  		sleep 1
			count=$(($count+1))
			if [ "$count" -eq 6 ]; then
				umount -a 2>/dev/null || :
				umount /mnt/tmp 2>/dev/null || : # /boot
				reboot -f
			fi
  	done
}

trap '_chk_err' 1 2 3 15 ERR

/bin/mount -t proc none /proc 
/bin/mount -n -t sysfs none /sys 
rm -f /etc/mtab
ln -s /proc/mounts /etc/mtab
ln -s /usr/bin/killall /bin/killall

/sbin/klogd -c 3

. /etc/scripts/env 
. /etc/scripts/utils.fn 
. /etc/scripts/boot.fn

export running_mode="hostinitram"

setlogcons 12
setterm -blank 0
setterm -store

echo "##"
echo "## initram-${mostaza_version} ..."

_rescue () {
	echo ""
	echo " RESCUE MODE CALLED "; sleep 1
	echo ""
	rm -f /root/.bashrc
	ln -s /root/.bashrc.rescue /root/.bashrc
	/bin/bash --rcfile /root/.bashrc
	echo 1 > /tmp/rescue_mode
}

# main
if [ "$(cat /proc/cmdline|grep -i rescue)" ]; then
	_rescue
fi
if [ ! -f "/tmp/rescue_mode" ]; then
	_step_one
	_open_root
	_open_other_mps
	_kill_pass_prompt
fi

mount -o remount,rw,sync /mnt/root
if [ "$installer_for" = "gentoo" ]; then
	_set_devtar
elif [ "$installer_for" = "slackware" ]; then
	# see /etc/rc.d/rc.S
	mkdir -p -m 700 /mnt/root/etc/lvm/backup 
fi

/etc/rc.d/rc.usb stop 2>/dev/null || :
/etc/rc.d/rc.ieee1394 stop 2>/dev/null || :
/etc/rc.d/rc.pcmcia stop 2>/dev/null || :
/etc/rc.d/rc.udev force-stop 2>/dev/null || :

# starting the exodus
trap - ERR
umount -rln /dev 
mount -o remount,ro /mnt/root
_msg "Saving boot log to /boot/boot-initram.log ..."
cp $logallfile /mnt/tmp/boot-initram.log
umount -rln /mnt/tmp # /boot for logging
umount -rln /proc
umount -rln /sys
set +eE
echo "##"
echo "## Calling /sbin/init ..."
exec switch_root /mnt/root /sbin/init $@
